/*--------------------------------------------------------------------*/
/* ce fichier illustre comment les petites allocations influencent la
/* consommation de mémoire virtuelle
/*------------ -------------------------------------------------------*/



/*--------------------------------------------------------------------*/
/*parametres: h=1,2,3,4,5,6,7,8
/*            2^n
/*--------------------------------------------------------------------*/
#include "DumperStatusProc.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


#define BLOC 4096

#define INNER_LOOP 1


int N=1024;





int main(int argc,char** argv){

  if(argc >=2){
     N = atoi(argv[1]);
  }
  else{
    printf("Usage: ./prog [taille loop]\n");
    return 0;
  }
  printf("---------\n");
  /*instancier le dumperStatus ici*/
  DumperStatusProc dp;
  
  void* p[INNER_LOOP];
  void* q[INNER_LOOP];
  void* r[INNER_LOOP];
  
#ifdef ALTERNED_ALLOC //(+)
  printf("Good allocation pattern\n");
  for(int i=0;i<N;i++){
    
    
    
    for(int j=0;j<INNER_LOOP;j++){
      /*sequence 1*/
      p[j]=malloc(BLOC);
      q[j]=malloc(BLOC);
    }
    
    for(int j=0;j<INNER_LOOP;j++){
      free  (p[j]);
      free  (q[j]);
    }
    
    for(int j=0;j<INNER_LOOP;j++){
      /*sequence 2*/
      r[j]=malloc(3*BLOC);
    }
    
    for(int j=0;j<INNER_LOOP;j++){
      r[j]= malloc(3*BLOC);
    }
    
  }
#endif
  
  
#ifdef SIMPLE_ALLOC //(-)
  printf("Bad allocation pattern\n");
  for(int i=0;i<N;i++){
    
    for(int j=0;j<INNER_LOOP;j++){
      /*sequence 1*/
      p[j]= malloc(BLOC);
      q[j]= malloc(BLOC);
      
    }
    
    for(int j=0;j<INNER_LOOP;j++){
      r[j]= malloc(3*BLOC);
    }
  
    for(int j=0;j<INNER_LOOP;j++){
      /*sequence 2*/
      free(p[j]);
      free(q[j]);
    }
    
    for(int j=0;j<INNER_LOOP;j++){
      r[j]= malloc(3*BLOC);
    }
  }
#endif
  status_t st = dp.getStatus();
  dp.printMeasure();
  
  printf("---------\n");
  return 0;
}
